package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorPatientDTO;
import com.pshcp.domain.model.DoctorPatient;
import com.pshcp.mapper.DoctorPatientMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生的患者表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorPatientService {

    @Autowired
    private DoctorPatientMapper doctorPatientMapper;

    public List<DoctorPatient> getDoctorPatientList(Long doctorId) {
        Example example = new Example(DoctorPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("relationFlag", 1);
        example.setOrderByClause(" created_time desc");
        List<DoctorPatient> list = doctorPatientMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorPatient> getDoctorPatientUngroupedPage(DoctorPatientDTO doctorPatientDTO) {
        List<DoctorPatient> list = doctorPatientMapper.getDoctorPatientUngroupedList(doctorPatientDTO);
        Long count = doctorPatientMapper.getDoctorPatientUngroupedListCount(doctorPatientDTO);
        PageList<DoctorPatient> pageList = CommonTool.buildPageList(doctorPatientDTO.getPageNum(), doctorPatientDTO.getPageSize(), list, count);
        return pageList;
    }

    public PageList<DoctorPatient> list(DoctorPatientDTO doctorPatientDTO) {
        if (doctorPatientDTO.getPageNum() != null && doctorPatientDTO.getPageSize() != null) {
            PageHelper.startPage(doctorPatientDTO.getPageNum(), doctorPatientDTO.getPageSize());
        }
        Example example = new Example(DoctorPatient.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorPatientDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorPatientDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (doctorPatientDTO.getDoctorId() != null) {
            criteria.andEqualTo("doctorId", doctorPatientDTO.getDoctorId());
        }
        if (doctorPatientDTO.getType() != null) {
            criteria.andEqualTo("type", doctorPatientDTO.getType());
        }
        if (doctorPatientDTO.getRelationFlag() != null) {
            criteria.andEqualTo("relationFlag", doctorPatientDTO.getRelationFlag());
        }
        //搜素
        if (!StringUtils.isBlank(doctorPatientDTO.getSearch())) {
            Example.Criteria criteria1 = example.createCriteria();
            if (doctorPatientDTO.getSearchType() == null || doctorPatientDTO.getSearchType() == 1) {
                criteria1.orLike("name", "%" + doctorPatientDTO.getSearch() + "%");
                criteria1.orLike("phone", "%" + doctorPatientDTO.getSearch() + "%");
                criteria1.orLike("remark", "%" + doctorPatientDTO.getSearch() + "%");
            } else {
                criteria1.orLike("name", "%" + doctorPatientDTO.getSearch() + "%");
            }
            example.and(criteria1);
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorPatient> list = doctorPatientMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorPatient> select(DoctorPatient doctorPatient) {
        return doctorPatientMapper.select(doctorPatient);
    }

    public int insert(DoctorPatient doctorPatient) {
        doctorPatient.setId(SystemUtil.getId());
        doctorPatient.setStatus(CommonStatusEnum.INIT.getCode());
        doctorPatient.setCreatedTime(new Date());
        return doctorPatientMapper.insert(doctorPatient);
    }

    public int update(DoctorPatient doctorPatient) {
        doctorPatient.setUpdateTime(new Date());
        return doctorPatientMapper.updateByPrimaryKeySelective(doctorPatient);
    }

    public DoctorPatient findById(Long id) {
        return doctorPatientMapper.selectByPrimaryKey(id);
    }

    public List<DoctorPatient> findByIds(List<Long> ids) {
        Example example = new Example(DoctorPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorPatientMapper.selectByExample(example);
    }

    public List<DoctorPatient> getDoctorPatientList(Long doctorId, List<Long> patientIds) {
        if (doctorId == null || CollectionUtils.isEmpty(patientIds)) {
            return Collections.emptyList();
        }
        Example example = new Example(DoctorPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andIn("patientId", patientIds);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        return doctorPatientMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorPatient doctorPatient = new DoctorPatient();
        doctorPatient.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorPatientMapper.updateByExampleSelective(doctorPatient, example);
    }

}
